package org.xidea.lite.servlet;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class DateUtils extends org.apache.commons.lang.time.DateUtils{

	private static Log log = LogFactory.getLog(DateUtils.class);

	@SuppressWarnings("unused")
	private static String defaultDatePattern = null;

	@SuppressWarnings("unused")
	private static String timePattern = "HH:mm";

	private DateUtils() {

	}

	/**
	 * This method generates a string representation of a date/time in the
	 * format you specify on input
	 * 
	 * @param aMask
	 *            the date pattern the string is in
	 * @param strDate
	 *            a string representation of a date
	 * @return a converted Date object
	 * @see java.text.SimpleDateFormat
	 * @throws ParseException
	 */
	public static final Date convertStringToDate(String aMask, String strDate)
			throws ParseException {
		SimpleDateFormat df = null;
		Date date = null;
		df = new SimpleDateFormat(aMask, Locale.getDefault());

		if (log.isDebugEnabled()) {
			log.debug("converting '" + strDate + "' to date with mask '"
					+ aMask + "'");
		}

		date = df.parse(strDate);

		return date;
	}


	public static int daysBetween(Date oneDate, Date toDate) {
		long l = oneDate.getTime() - toDate.getTime();
		int nDay = (int) (l / (24 * 60 * 60 * 1000));
		return nDay;
	}

	/*
	 * 获取指定日期所在周的周日

	 * 
	 */
	public static Date getSundayOfWeek(Date date) {
		date = getMondayOfWeek(date);
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		cal.setTime(date);
		cal.add(Calendar.DATE, 6);
		return cal.getTime();
	}

	/*
	 * 获取指定日期所在周的周一
	 */
	public static Date getMondayOfWeek(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int result = cal.get(Calendar.DAY_OF_WEEK);
		if (result == 1) {
			return getPreOrNextDay(date, -6);
		}
		cal.add(Calendar.DATE, -(result - 2));
		return cal.getTime();

	}

	/*
	 * 获取指定日期的前数天或下数天
	 */
	public static Date getPreOrNextDay(Date date, int num) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, num);
		return cal.getTime();

	}
	
	/*
	 * 获取指定日期为星期几
	 */
	public static String getXingQi(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		String rtInf="";
		int result = cal.get(Calendar.DAY_OF_WEEK);
		switch(result){
			case 1:
				rtInf="星期日";
				break;
			case 2:
				rtInf="星期一";
				break;
			case 3:
				rtInf="星期二";
				break;
			case 4:
				rtInf="星期三";
				break;
			case 5:
				rtInf="星期四";
				break;
			case 6:
				rtInf="星期五";
				break;
			case 7:
				rtInf="星期六";
				break;
		}
		return rtInf;

	}

	static public String getDateStr() {
		Date date=new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
		return format.format(date);
	}
	
	//返回作为文件名
	public static String getFolder() {
		Calendar upTime = new GregorianCalendar();
		DateFormat df = new SimpleDateFormat("yyyyMMdd");
		return df.format(upTime.getTime());
	}
	public static String getMonFolder() {
		Calendar upTime = new GregorianCalendar();
		DateFormat df = new SimpleDateFormat("yyyyMM");
		return df.format(upTime.getTime());
	}
	public static String getfileName() {
		Calendar upTime = new GregorianCalendar();
		DateFormat df = new SimpleDateFormat("HHmmssSSS");
		return df.format(upTime.getTime());
	}
	public static String getDayfileName() {
		Calendar upTime = new GregorianCalendar();
		DateFormat df = new SimpleDateFormat("ddHHmmssSSS");
		return df.format(upTime.getTime());
	}
	//获取年份
	public static String getYearStr(){
		Date date=new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String str = format.format(date);
		return str.substring(0, 4);
	}

}
